<?php
/*
Plugin Name: Side Banner (Curious)
Description: Simple widget to display banner advertisement on sidebar.
Version: 1.0.0
Author: gnuinne
Author URI: http://gnuinne.com
*/

class Side_Banner_Widget extends WP_Widget {
  function side_banner_Widget() {
    $widget_ops = array('classname' => 'widget_side_banner', 'description' => __('Display Banner Advertisement on your sidebar'));
    $control_ops = array('width' => 400, 'height' => 350);
    $this->WP_Widget('side_banner', __('Side Banner'), $widget_ops, $control_ops);
  }
  
  function widget($args, $instance) {
    extract($args);
    $text = $instance['text'];
    $chunks = explode('<!--more-->', $text);
    $chunkno = mt_rand(0, sizeof($chunks) - 1);

    echo $before_widget;
    if (!empty($title)) echo $before_title . $title . $after_title;
    echo '<div class="widget_side_banner">' . trim($chunks[$chunkno]) . '</div>';
    
    echo $after_widget;
  }
  
  function update($new_instance, $old_instance) {
    $instance = $old_instance;
    if (current_user_can('unfiltered_html'))
      $instance['text'] = $new_instance['text'];
    else
      $instance['text'] = wp_filter_post_kses($new_instance['text']);

    return $instance;
  }

  function form($instance) {
    $text = format_to_edit($instance['text']);
?>    
    
    <p><label for="<?php echo $this->get_field_id('text'); ?>">Banner Code (HTML)</label></p>
    <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<?php
  }
}

function register_Side_Banner_Widget(){
  register_widget('Side_Banner_Widget');
}

add_action('init', 'register_Side_Banner_Widget', 1);

?>